package com.test

import com.entity.PackPath
import com.subpackage.BasePack
import com.utils.PropertiesUtils
import org.json.JSONArray
import java.nio.charset.Charset
import java.nio.file.FileSystems
import java.util.regex.Pattern
import kotlin.test.Test

class JavaTest {


    @Test
    fun te() {

//        val path = "E:\\Project\\packTest\\old\\.DS_Store"
//        val ignore = mutableListOf("**/.svn", "**/.git", "**/.DS_Store")
//        val result = ignore.any {
//            BasePack().validatePath(path, it)
//        }
        println(System.getProperty("user.dir"))

        println(FileSystems.getDefault().separator)
        println(FileSystems.getDefault().rootDirectories)
        println(FileSystems.getDefault().fileStores.joinToString { it.name() })
        println(FileSystems.getDefault().provider().scheme)
        println(FileSystems.getDefault().supportedFileAttributeViews())

    }


    @Test
    fun test3() {
        val charset = Charset.defaultCharset()
        println(charset.name())
        var s = "{key: 'config_' + language + '.js', v: 1}"
        s = s.replace("config_' + language + '.js", "config.js")
        println(s)
    }

    @Test
    fun fileTest() {
        System.setProperty("file.separator", "/")
        println(FileSystems.getDefault().separator)
        val file = PackPath("E:\\WorkSpace\\IDEA\\Java\\PackProject\\data\\script/con_a.js")
        val file2 = PackPath("E:/WorkSpace/IDEA/Java/PackProject/data/script/con_a.js")

        println("${file.pathString} \n${file2.pathString}\n${file.pathString}\n${file.absolutePathString}")
//        println(file.pathString.matches("con*.js".toRegex()))
        //file.exists()
    }

    @Test
    fun regTest() {
        System.setProperty("rootDir", "E:\\WorkSpace\\IDEA\\Java\\PackProject\\data")
        val regex = "\\{.*}"
        val content = """var arr = [
    {key: 'aes.js', v: 1},
    {key: 'client.js', v: 1},
    {key: 'config.js', v: 1},
    {key: 'game.js', v: 1},
    {key: 'script.js', v: 1},
    {key: 'gameSdk.js', v: 1}
];

let language = getQueryString("language");
if (language) {
    arr.push({key: 'config_' + language + '.js', v: 1},)
} else {
    arr.push({key: 'config.js', v: 1},)
}
arr.push({key: 'main.min.js', v: 1})"""


        val pattern = Pattern.compile(regex)
        val matcher = pattern.matcher(content)
        while (matcher.find()) {
            if (matcher.group().isNotEmpty()) {
                println(matcher.group())
            }
        }


        println("--------------------")

        val a = "\\{key..+,(\\s+)?v.(\\s+)?\\d+}".toRegex().findAll(content).map {
            it.value
        }

        println(a.joinToString())

    }

    @Test
    fun test2() {

//        File file = new File("./.aws_user2");
        val file = PackPath("pom.xml")
//        File file = new File("E:\\WorkSpace\\IDEA\\Java\\PackProject\\data\\lua\\config.lua");
        println("test2 = " + file.isAbsolute)
        println("test2 = " + file.parent)
        println("test2 = " + file.parent?.exists())
        println("test2 = " + file.parent?.createParentDirectories())
        println("test2 = " + file.toAbsolutePath())
        println("test2 = " + file.name)
        println("test2 = " + file.nameWithoutExtension)
        println("test2 = " + file.extension)
        println("test2 = " + file.parent?.toAbsolutePath())
        println("test2 " + file.exists())

//        Base64.encodeBase64String(name.getBytes())

//        String key = "WW1WemRGTm9iMjkwWlhJdlUzUmhhMlZXYVdWM0xuaHRiSHg0TjNCNmVHVXllbTl1WW5FM2RpMXVNVjltYW1WdXxaSEpsWVcxVWNtbHdMMU4wWVd0bFZtbGxkeTU0Yld4OE9XNW1NMjFrWW1wMGRqUmpNeTF1TVY5bWFtVnV8Wm1GemRGTndhVzR2UW05MGRHOXRWbWxsZHk1NGJXeDhhelJsYkdkMFkzWnVOR1ZqZHkxdU9GOW1kM1I0fFptRnpkRk53YVc0dlUzUmhhMlZXYVdWM0xuaHRiSHhyTkdWc1ozUmpkbTQwWldONExXNHhYMlpxWlc0PXxabTl2ZEdKaGJHeERkWEF2UW05MGRHOXRWbWxsZHk1NGJXeDhaV014Y3pBNVpHSnJPWGc1WlMxdU9GOW1kM1I0fFptOXZkR0poYkd4RGRYQXZVM1JoYTJWV2FXVjNMbmh0Ykh4bFl6RnpNRGxrWW1zNWVEbG1MVzR4WDJacVpXND18Wm5KMWFYUjVSbTl2ZEdKaGJHd3ZRbTkwZEc5dFZtbGxkeTU0Yld4OGRteHlaSGgyT1ROd2JqaGxPUzF1T0Y5bWQzUjR8Wm5KMWFYUjVSbTl2ZEdKaG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qZW58M3FheTFmMHZvZnV6cy1uMV9mamVufHN0cWtuZXdkYjhwOXEtbjFfZmplbnxpY2NyemFxNGZqZW50LW4xX2ZqZW58bmgwMm13c3F6MW9tNy1uMV9mamVufDZnY3Z0MzFsanNwdzFwLW4xX2ZqZW58d2Y2d2FyZGJvOTRwYy1uOF9md3R4fHdmNndhcmRiZHFqMDVzLW4xX2ZqZW58eHMwdzYxcTJxdzVmNWEtbjcwX3ZiNDl8eHMwdzYxcTJ2YjQ5OXItbjFfZmplbg==";
//        byte[] bb = Base64.decodeBase64(key.getBytes());
//        String key2 = new String(bb);
//        String[] string = StringUtils.split(key2, "|");
//        for (int i = 0; i < string.length; i++) {
//            System.out.println(string[i]);
////            bb = Base64.decodeBase64(string[i].getBytes());
////            System.out.println( new String(bb) );
//        }
    }

    @Test
    fun testCollections4() {
        val list1: MutableList<String?> = ArrayList()
        val list2: MutableList<String?> = ArrayList()
        list1.add("aa")
        list1.add("bb")
        list2.add("aa")
        list2.add("cc")

        // 合并list
        val list = list1 + list2
        println(list.joinToString(","))
        // list1与list2的差集
        val sub1 = list1.subtract(list2)
        println(sub1.joinToString(","))
        // list2与list1的差集
        val sub2 = list2.subtract(list1)
        println(sub2.joinToString(","))
        // 取出列表中多余的值
        val sum = (list2 + list1).distinct()
        println(sum.joinToString(","))
    }

    @Test
    fun testPropertiesUtils() {
        try {
            PropertiesUtils.instance.read("/Users/boge/Project/test2/abcd.txt")
            PropertiesUtils.instance.add("a", "v")
            PropertiesUtils.instance.add("a", "w")
            PropertiesUtils.instance.store()
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @Test
    fun changejs() {
        val path = PackPath("E:\\项目资源\\index.js")
        if (path.notExists()) return
        var str = path.readText()
        var changeStr = str.substringAfter("[")
        changeStr = changeStr.substringBefore("]")
        changeStr = "[$changeStr]"
        val json = JSONArray(changeStr)
        json.getJSONObject(0).optString("key")
        json.getJSONObject(0).optInt("v")
        //json.getJSONObject(0).putOpt()
        str = str.replace(changeStr, json.toString())
        println(str)
    }

    @Test
    fun testDate() {
        val ti: Long = 86400
        println(ti / (24 * 60 * 60))
        println(24 * 60 * 60)
    }

    @Test
    fun sss() {
        sss2("dd")
        sss2("\r")
        sss2("\n")
        sss2(" ")
        println(
            "E:\\WorkSpace\\IDEA\\Java\\PackProject\\src\\test\\java\\com\\test\\AESTest.java".contains("test\\")
        )
    }

    private fun sss2(str: String) {
        println(str.contains("\r"))
    }

    @Test
    fun dom4jTest() {
        val file = PackPath("E:\\WorkSpace\\IDEA\\Java\\PackProject\\src\\test\\en.xml")
        BasePack().formatXml(file, file.parent?.resolve("newEn.xml"))
    }
}
